package org.ronghao.androidwifiplatfrom.entity;

import org.json.JSONException;
import org.json.JSONObject;
import org.ronghao.androidwifiplatfrom.utils.Contants;

public class MessageQueue {
	private int index = 0;// 消息队列
	private String sendClientIp;// 发送消息的客户端的IP地址
	private String sendClientName;// 发送消息的客户端的名称
	private int type;// 类型：广播 ，点对点
	private String action;// 动作
	private String msgData;// 数据：以后的游戏数据

	public MessageQueue() {
	}

	public MessageQueue(int index, String sendClientIp, String sendClientName,
			int type, String action, String msgData) {
		this.index = index;
		this.sendClientIp = sendClientIp;
		this.sendClientName = sendClientName;
		this.type = type;
		this.action = action;
		this.msgData = msgData;
	}

	public static MessageQueue createMessageQueue(String msg) {
		MessageQueue messageQueue = new MessageQueue();
		try {
			JSONObject jsonObject = new JSONObject(msg);
			messageQueue.setIndex(jsonObject.getInt(Contants.COMMUNICATION_INDEX));
			messageQueue
					.setSendClientIp(jsonObject.getString(Contants.COMMUNICATION_SEND_CLIENT_IP));
			messageQueue.setSendClientName(jsonObject
					.getString(Contants.COMMUNICATION_SEND_CLIENT_NAME));
			messageQueue.setType(jsonObject.getInt(Contants.COMMUNICATION_TYPE));
			messageQueue.setAction(jsonObject.getString(Contants.COMMUNICATION_ACTION));
			messageQueue.setMsgData(jsonObject.getString(Contants.COMMUNICATION_MSG_DATA));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return messageQueue;
	}

	/**
	 * 将Message 转换成JSON字符串
	 * @author ronghao
	 * @data 2012-11-19    上午11:25:02
	 * @return
	 */
	public String messageQueuetoString() {
		// return "{" + "\"index\":" + index + ",\"send_client_ip\":\"" +
		// sendClientIp
		// + "\",\"send_client_name\":\"" + sendClientName + "\",\"type\":" +
		// type
		// + ",\"action\":\"" + action + "\",\"msg_data\":\"" + msgData + "\"}";
		return Contants.objToJsonString(this);
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public String getSendClientIp() {
		return sendClientIp;
	}

	public void setSendClientIp(String sendClientIp) {
		this.sendClientIp = sendClientIp;
	}

	public String getSendClientName() {
		return sendClientName;
	}

	public void setSendClientName(String sendClientName) {
		this.sendClientName = sendClientName;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getMsgData() {
		return msgData;
	}

	public void setMsgData(String msgData) {
		this.msgData = msgData;
	}
}
